import Modal from "antd/es/modal/Modal";
import {useEffect, useState} from "react";
import {Form, Input, message, Switch} from "antd";
import TextArea from "antd/es/input/TextArea";
import http from "../../../utils/http";

const TagModal = (props) => {
    const [form] = Form.useForm();
    const [isModalVisible,setIsModalVisible] = useState(false);
    const [data,setData] = useState({});
    const formData = props.data;

    useEffect(()=>{
        setIsModalVisible(props.isShow);
        setData(props.data);
    },[props]);

    form.setFieldsValue(formData);

    const handleCancel = () => {
        setIsModalVisible(false);
        setData(props.data);
        props.close();
    }
    const handleOk = () => {
        http({
            url:'api/user/updateLabel',
            method:'post',
            params:formData
        }).then(res=>{
            console.log(res);
            if (res.code == 200){
                message.success('修改成功');
            }
        })
        console.log(formData)
        setIsModalVisible(false);
        props.close();
    }
    return (
        <Modal title="修改" visible={isModalVisible} onOk={handleOk} onCancel={handleCancel}>
            <Form
                name="basic"
                labelCol={{span: 8}}
                wrapperCol={{span: 16}}
                form={form}
                initialValues={formData}
                autoComplete="off"
            >
                <Form.Item name={'name'} label={'标签名称'} rules={[{required:true,message:'请输入标签名称'}]}>
                    <Input
                        placeholder={'请输入标签名称'}
                        value={formData.name}
                        onChange={(ev) => {formData.name = ev.target.value}}
                    />
                </Form.Item>
                <Form.Item name={'discount'} label={'会员折扣'} rules={[{required:true,message:'请输入会员折扣（0-1）'}]}>
                    <Input
                        placeholder={'请输入会员折扣（0-1）'}
                        value={formData.discount}
                        onChange={(ev) => {formData.discount = ev.target.value}}
                    />
                </Form.Item>
                <Form.Item name={'overview'} label={'等级简述'} rules={[{required:true,message:'请输入等级简述'}]}>
                    <TextArea
                        value={formData.overview}
                        onChange={(ev)=>{formData.overview = ev.target.value}}
                        placeholder='请输入等级简述'
                        autoSize={{ minRows: 3, maxRows: 5 }}
                    />
                </Form.Item>
                <Form.Item label="等级简述" name="status">
                    <Switch defaultChecked={formData.status} onChange={(val)=>{formData.status = val}} />
                </Form.Item>
            </Form>
        </Modal>
    )
}

export default TagModal;